﻿using AdminStyem.Application.Command.SlaughterCommand;
using AdminStyem.Application.DTO;
using AdminStyem.Domain.Entity;
using AdminStyem.Repositories;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.SlaughterCommandHandler
{
    public class SlaughterShowCommandHandler : IRequestHandler<SlaughterShowCommand, APIResult<List<SlaughterDTO>>>
    {
        IBaseRespository<Slaughter> BaseSlaughter;

        public SlaughterShowCommandHandler(IBaseRespository<Slaughter> baseSlaughter)
        {
            BaseSlaughter = baseSlaughter;
        }

        public Task<APIResult<List<SlaughterDTO>>> Handle(SlaughterShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<SlaughterDTO>> ret = new APIResult<List<SlaughterDTO>>();
            ret.Code = ResultCode.查询成功;
            ret.Message = ResultCode.查询成功.ToString();

            var slaughterList = BaseSlaughter.Queryable()
                .Select(s => new SlaughterDTO
                {
                    SlaughterBatch = s.SlaughterBatch,
                    SlaughterWeight = s.SlaughterWeight,
                    Dtime = s.Dtime,
                    SlaughterTime = s.SlaughterTime,
                    IsDeleted = s.IsDeleted
                }).ToList();

            ret.Data = slaughterList;

            return Task.FromResult(ret);
        }
    }
}
